package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.dto.Result;
import com.music.entity.SongList;
import com.music.entity.SongListType;
import com.music.service.SongListService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/songlist")
@Slf4j
public class SongListController {

    private static final Logger logger = LoggerFactory.getLogger(SongListController.class);

    @Autowired
    private SongListService songListService;

    @GetMapping
    public @ResponseBody
    Result getSongList(String songListName, String songListTypeName,
                       @RequestParam Integer pageNum, @RequestParam Integer pageSize,
                       String rank,
                       HttpServletResponse response) throws IOException{
        //设置统一结果返回
        Result result = new Result();

        SongList songList = null;

        if (songListName != null || songListTypeName != null || rank != null){
            songList = new SongList();
        }
        if(songListName != null && !"".equals(songListName)){
            songList.setSongListName(songListName);
        }
        if(songListTypeName != null && !"".equals(songListTypeName)){
            SongListType songListType = new SongListType();
            songListType.setSongListTypeName(songListTypeName);
            songList.setSongListType(songListType);
        }
        PageInfo<SongList> songListDetailByPage = songListService.getSongListDetailByPage(songList,pageNum,pageSize,rank);

        if(!songListDetailByPage.getList().isEmpty()){
            result.ok(songListDetailByPage);
        }else{
            result.ok(songListDetailByPage).setMessage("没有相关数据");
        }
        return result;
    }
    @RequestMapping("/all")
    public @ResponseBody Result dofindall(Integer songListTypeId,String songListName,String songListId){
        Result result = new Result();
        List<SongList> list=new ArrayList<SongList>();
        SongList songList=new SongList();
        if(songListTypeId !=null && !"".equals(songListTypeId)){
            songList.setSongListTypeId(songListTypeId);
        }
        if(songListName !=null && !"".equals(songListName)){
            songList.setSongListName(songListName);
        }
        if(songListId !=null && !"".equals(songListId)){
            songList.setSongListId(songListId);
        }
        list=songListService.getSongList(songList);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"查无数据");
        }
        return result;
    }
    @RequestMapping("/page")
    public @ResponseBody Result dopage(String songListName, Integer songListTypeId, Integer songListStatus,@RequestParam int pageNum,@RequestParam int pageSize){
        Result result = new Result();
        PageInfo<SongList> list=songListService.findByCondition(songListName,songListTypeId,songListStatus,pageNum,pageSize);
        if(!list.getList().isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }
    @RequestMapping("/update")
    public @ResponseBody Result doUpdate(@RequestParam Integer songListStatus,@RequestParam String songListId){
        Result result = new Result();
        int a=songListService.updateSongList(songListStatus,songListId);
        if(a>0){
            result.ok(true);
        }else{
            result.fail(0,"修改失败");
        }
        return result;
    }

}
